package org.dromara.web.plugins.file;


import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.util.List;

@Component
public class DOCFileResolver implements FileResolver{


    @Override
    public String resolve(String path) {
        try {
            StringBuffer sb = new StringBuffer();
            // 创建文件输入流对象
            FileInputStream fis = new FileInputStream(path);
            // 创建XWPFDocument对象
            XWPFDocument document = new XWPFDocument(fis);
            // 获取段落列表
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            // 遍历段落并打印文本
            for (XWPFParagraph paragraph : paragraphs) {
                sb.append(paragraph.getText());
            }
            // 关闭文档和文件输入流
            document.close();
            fis.close();
            return sb.toString();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public String[] supportedExtensions() {
        return new String[]{"doc", "docx"};
    }
}
